package x.ovo.wechat.bot;

import x.ovo.wechat.bot.common.entity.Contact;
import x.ovo.wechat.bot.common.entity.event.MessageEvent;
import x.ovo.wechat.bot.common.entity.message.ImageEmoteMessage;
import x.ovo.wechat.bot.common.entity.message.Message;
import x.ovo.wechat.bot.common.entity.message.TextMessage;
import x.ovo.wechat.bot.common.event.Event;
import x.ovo.wechat.bot.common.event.EventListener;

import java.util.List;
import java.util.Map;

public class ComeDogListener extends EventListener<MessageEvent<Message>> {

    @Override
    public boolean support(Event<?> event) {
        return event instanceof MessageEvent;
    }

    @Override
    @SuppressWarnings("unchecked")
    public void onEvent(MessageEvent<Message> event) {
        String content = event.getContent();
        Contact fromUser = event.getSource().getFromUser();

        // 群聊内艾特机器人消息
        if (fromUser.isGroup() && event.getSource() instanceof TextMessage && ((TextMessage) event.getSource()).isAtMe()) {
            this.context.getApi().sendEmoticon(fromUser.getUserName(), "bdbcf4e9f0b4001ee1576d75a0b391bf");
            return;
        }

        // 如果消息是图片表情消息且不是群聊消息，则返回表情的md5
        if (event.getSource() instanceof ImageEmoteMessage && !fromUser.isGroup()) {
            this.context.getApi().sendText(fromUser.getUserName(), ((ImageEmoteMessage) event.getSource()).getMd5());
            return;
        }

        if (this.plugin.getConfig().getStringList("blacklist").stream().anyMatch(content::contains)) {
            return;
        }
        // 处理content
        String s = content.replaceAll("@.* ", "").replaceAll("「.*」<br/>- - - - - - - - - - - - - - -<br/>", "");
        List<Map<String, Object>> data = (List<Map<String, Object>>) this.plugin.getConfig().getList("data");
        data.forEach(map -> ((List<String>) map.get("keyword")).forEach(keyword -> {
            if (s.contains(keyword)) {
                this.context.getApi().sendEmoticon(fromUser.getUserName(), (String) map.get("md5"));
            }
        }));

    }
}
